package com.example.carproviderbackstage.service.impl.dl.admin;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.carinterface.service.dl.admin.CouponService;
import com.example.carinterface.utils.ResultJson;
import com.example.carproviderbackstage.mapper.dl.admin.CouponMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private ResultJson<String> stringResultJson;

    @Override
    public ResultJson<List<Map<String, Object>>> find(Map<String,Object> map) {
        if (map==null){
            map = new HashMap<>();
        }
        ResultJson<List<Map<String, Object>>> resultJson = new ResultJson<>();
        List<Map<String, Object>> list = couponMapper.find(map);
        resultJson.setSuccess("查询成功", list);
        return resultJson;
    }

    @Override
    public ResultJson<String> add(Map<String, Object> map) {
        ResultJson<String> resultJson = new ResultJson<>();
        int i = couponMapper.add(map);
        if (i == 1) {
            resultJson.setSuccess("添加成功");
        } else {
            resultJson.setSuccess("添加失败");
        }
        return resultJson;
    }

    @Override
    public ResultJson<String> updateCoupon(Map<String, Object> map) {
        ResultJson<String> resultJson = new ResultJson<>();
        int i = couponMapper.updateCoupon(map);
        if (i == 1) {
            resultJson.setSuccess("修改成功");
        } else {
            resultJson.setSuccess("修改失败");
        }
        return resultJson;
    }

    @Override
    public ResultJson<List<Map<String, Object>>> findExchangeCode() {
        ResultJson<List<Map<String, Object>>> resultJson = new ResultJson<>();

        List<Map<String, Object>> list = couponMapper.findExchangeCode();
        resultJson.setSuccess("查询成功", list);
        return resultJson;
    }

    @Override
    public ResultJson<String> addExchangeCode(Map<String, Object> map) {
        ResultJson<String> resultJson = new ResultJson<>();

        int i = couponMapper.addExchangeCode(map);
        if (i == 1) {
            resultJson.setSuccess("添加成功");
        } else {
            resultJson.setSuccess("添加失败");
        }
        return resultJson;
    }

    @Override
    public ResultJson<String> updateExchangeCode(Map<String, Object> map) {
        ResultJson<String> resultJson = new ResultJson<>();

        int i = couponMapper.updateExchangeCode(map);
        if (i == 1) {
            resultJson.setSuccess("修改成功");
        } else {
            resultJson.setSuccess("修改失败");
        }
        return resultJson;
    }

    @Override
    public ResultJson<String> delExchangeCode(Integer id) {
        ResultJson<String> resultJson = new ResultJson<>();

        int i = couponMapper.delExchangeCode(id);
        if (i == 1) {
            resultJson.setSuccess("删除成功");
        } else {
            resultJson.setSuccess("删除失败");
        }
        return resultJson;
    }

    @Override
    public ResultJson<String> testTel(String tel) {
        ResultJson<String> resultJson = new ResultJson<>();
       Map<String,Object> user =  couponMapper.findExistedUserByTel(tel);
       if (user!=null){
           resultJson.setSuccess("检测成功");
       }else {
           resultJson.setFail("未找到该用户，请检测手机号");
       }
        return resultJson;
    }

    @Override
    public ResultJson<String> updateState(Map<String, Object> map) {
        map.putIfAbsent("state", -1);

       int i = couponMapper.updateStateCoupon(map);
       if (i==1){
           stringResultJson.setSuccess("修改成功");
       }else {
           stringResultJson.setSuccess("修改失败");
       }
        return stringResultJson;
    }

    @Override
    public ResultJson<String> delCoupon(Integer id) {

        int i =  couponMapper.delCoupon(id);

        if (i==1){
            stringResultJson.setSuccess("删除成功");
        }else {
            stringResultJson.setSuccess("删除失败");
        }


        return stringResultJson;
    }
}
